package com.lib.framework.manager

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.text.TextUtils

/**
 * 本应用 正在运行的 activity 管理
 *
 * @author keanbin
 */
object RunningActivityManager {

    private var mActivityList = mutableListOf<Activity>()

    fun init(application: Application) {
        application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityPaused(activity: Activity?) {

            }

            override fun onActivityResumed(activity: Activity?) {

            }

            override fun onActivityStarted(activity: Activity?) {

            }

            override fun onActivityDestroyed(activity: Activity?) {
                activity?.let {
                    mActivityList.remove(it)
                }
            }

            override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {

            }

            override fun onActivityStopped(activity: Activity?) {

            }

            override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
                activity?.let {
                    mActivityList.add(it)
                }
            }
        })
    }

    /**
     * 判断某个页面是否在启动
     *
     * @param classCanonicalName
     * @return
     */
    fun isRunning(classCanonicalName: String?): Boolean {
        if (TextUtils.isEmpty(classCanonicalName)) {
            return false
        }

        for (activity in mActivityList) {
            if (classCanonicalName == activity.javaClass.canonicalName) {
                return true
            }
        }

        return false
    }

    /**
     * 关闭除了指定页面外的其他所有页面
     *
     * @param classCanonicalName 不关闭的页面
     * @return
     */
    fun finishAllActivity(classCanonicalName: String? = null) {
        val list = mutableListOf<String>()
        classCanonicalName?.let {
            list.add(classCanonicalName)
        }
        finishAllActivity(list)
    }

    /**
     * 关闭除了指定页面外的其他所有页面
     *
     * @param classCanonicalNameList 不关闭的页面列表
     * @return
     */
    fun finishAllActivity(classCanonicalNameList: List<String>) {
        val activityList = mutableListOf<Activity>()
        if (mActivityList.size > 0) {
            activityList.addAll(mActivityList)
        }
        for (activity in activityList) {
            if (activity.javaClass.canonicalName !in classCanonicalNameList) {
                activity.finish()
            }
        }
    }

    /**
     * 关闭指定页面
     *
     * @param classCanonicalName
     * @return
     */
    fun finishActivity(classCanonicalName: String? = null) {
        val activityList = mutableListOf<Activity>()
        if (mActivityList.size > 0) {
            activityList.addAll(mActivityList)
        }
        for (activity in activityList) {
            if (classCanonicalName == activity.javaClass.canonicalName) {
                activity.finish()
            }
        }
    }

    /**
     * 判断某个页面是否是本应用所有打开页面的top
     *
     * @param classCanonicalName
     * @return
     */
    fun isTop(classCanonicalName: String?): Boolean {
        if (classCanonicalName.isNullOrEmpty()) {
            return false
        }

        val index = mActivityList.size - 1
        if (index in 0..mActivityList.size) {
            if (classCanonicalName == mActivityList[index].javaClass.canonicalName) {
                return true
            }
        }

        return false
    }
}